package com.cci.kangdao.controller;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dto.BaseResponse;
import com.cci.kangdao.model.CurrentFaultsInfo;
import com.cci.kangdao.model.FuelTripRecordInfo;
import com.cci.kangdao.service.CustomerEngineRelLoadMsgRelationService;
import com.cci.kangdao.utilTool.ParameterTool;
import com.gexin.fastjson.JSON;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CustomerEngineRelLoadMsgRelationController {

	private Logger log = Logger.getLogger(this.getClass().getName());

	@Autowired
	private CustomerEngineRelLoadMsgRelationService customerEngineRelLoadMsgRelationService;


	/**
	 * 提交载重信息（油耗导航）
	 * @param paramStr
	 * @return
	 */
	@RequestMapping("/submitLoadMsg.aspx")
	public ModelAndView submitLoadMsg( HttpServletRequest request,
											HttpServletResponse response) {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelLoadMsgRelationService.submitLoadMsg(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}


	/**
	 * 获取车辆历史载重信息记录中最新载重信息（油耗导航）
	 * @param request
	 * @return
	 */
	@RequestMapping("/getLoadMsgByCustomerEngineRelId.aspx")
	public ModelAndView getLoadMsgByCustomerEngineRelId(HttpServletRequest request,
											 HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelLoadMsgRelationService.getLoadMsgByCustomerEngineRelId(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	/**
	 * 是否保存为常规路线（油耗导航）
	 *
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/getLoadMsg.aspx")
	@ResponseBody
	public BaseResponse isLoadMsgFlag(@RequestParam("id") Long id, HttpServletResponse response) {
		if (null == id) {
			return BaseResponse.error("请输入id");
		}
		return customerEngineRelLoadMsgRelationService.isLoadMsgFlag(id);
	}


	/**
	 * 是否保存为常规路线（油耗导航）
	 *
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/updateLoadMsgNum.aspx")
	@ResponseBody
	public BaseResponse updateLoadMsgNum(Long id, HttpServletResponse response) {
		if (null == id) {
			return BaseResponse.error("请输入id");
		}
		return customerEngineRelLoadMsgRelationService.updateLoadMsgNum(id);
	}
	@RequestMapping("/updateLoadMsgComments.aspx")
	@ResponseBody
	public BaseResponse updateLoadMsgComments(@RequestParam Map<String, Object> map) {
		Long id = MapUtils.getLong(map, "ID");
		Integer score = MapUtils.getInteger(map, "Score");
		if (null == id) {
			return BaseResponse.error(-99, "请输入id");
		}
		if (null == score) {
			return BaseResponse.error(-99, "请选择评分");
		}
		return customerEngineRelLoadMsgRelationService.updateLoadMsgComments(map);
	}
	/**
	 * 搜索历史行程记录
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getLoadMsgSearch.aspx")
	@ResponseBody
	public BaseResponse getLoadMsg(HttpServletRequest request,HttpServletResponse response) {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			return customerEngineRelLoadMsgRelationService.getLoadMsgSearch(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * 获取 历史行程记录
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getLoadMsgHistory.aspx")
	public ModelAndView getLoadMsgHistory(HttpServletRequest request,HttpServletResponse response) {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response",response);
			return customerEngineRelLoadMsgRelationService.getLoadMsgHistory(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * 提交油耗导航行程数据记录（油耗导航）
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/submitFuelTrip.aspx",method = RequestMethod.POST)
	@ResponseBody
	public BaseResponse submitFuelTrip(@RequestBody String paramStr, HttpServletRequest request,
									  HttpServletResponse response) {
		try{
			log.info("submitFuelTrip"+paramStr);
			if (StringUtils.isEmpty(paramStr)) {
				return BaseResponse.error(-99, "缺少必要参数");
			}
			boolean success = customerEngineRelLoadMsgRelationService.submitFuelTrip(paramStr);
			if(success){
				return BaseResponse.success("保存成功");
			} else {
				return BaseResponse.error(-99, "保存失败");
			}
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * 现行故障码
	 * @param esn
	 * @return
	 */
	@RequestMapping(value = "/getCurrentFaultsbyEsn.aspx")
	@ResponseBody
	public BaseResponse getCurrentFaultsbyEsn(String esn, String startTime, HttpServletRequest request,
									   HttpServletResponse response) {
		try{
			if (StringUtils.isEmpty(esn) || StringUtils.isEmpty(startTime)) {
				return BaseResponse.error(-99, "缺少必要参数");
			}
			List<CurrentFaultsInfo> list = customerEngineRelLoadMsgRelationService.getCurrentFaultsbyEsn(esn,startTime);
			Map<String, Object> map = new HashMap<>();
			map.put("faultCodeList", list);
			return BaseResponse.success(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * 更新油耗导航行程数据记录（油耗导航）
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/updateFuelTrip.aspx",method = RequestMethod.POST)
	@ResponseBody
	public BaseResponse updateFuelTrip(@RequestBody String paramStr, HttpServletRequest request,
									   HttpServletResponse response) {
		try{
			log.info("updateFuelTrip"+paramStr);
			if (StringUtils.isEmpty(paramStr)) {
				return BaseResponse.error(-99, "缺少必要参数");
			}
			FuelTripRecordInfo info = JSON.parseObject(paramStr, FuelTripRecordInfo.class);
			UserT userT = UserThreadLocal.get();
			info.setUserId(userT.getId());
			boolean success = customerEngineRelLoadMsgRelationService.updateFuelTrip(info);
			if(success){
				return BaseResponse.success("保存成功");
			} else {
				return BaseResponse.error(-99, "保存失败");
			}
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * 根据id油耗导航行程数据记录（油耗导航）
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getFuelTripById.aspx")
	@ResponseBody
	public BaseResponse getFuelTripById(Long id, HttpServletRequest request,
									   HttpServletResponse response) {
		try{
			if (null == id) {
				return BaseResponse.error(-99, "缺少必要参数");
			}
			FuelTripRecordInfo info = customerEngineRelLoadMsgRelationService.getFuelTripById(id);
			Map<String, Object> map = new HashMap<>();
			map.put("info", info);
			return BaseResponse.success(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * 更新油耗导航驾驶行为建议（cip调用）
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/updateFuelTripSuggest.aspx",method = RequestMethod.POST)
	@ResponseBody
	public BaseResponse updateFuelTripSuggest(@RequestBody String paramStr, HttpServletRequest request, HttpServletResponse response) {
		try{
			log.info("updateFuelTripSuggest"+paramStr);
			if (StringUtils.isEmpty(paramStr)) {
				return BaseResponse.error(-99, "缺少必要参数");
			}
			FuelTripRecordInfo info = JSON.parseObject(paramStr, FuelTripRecordInfo.class);
			String source = request.getHeader("source");
			String version = request.getHeader("version");
			String token = request.getHeader("token");
			log.info("token:"+token +";source:" + source);
			if(StringUtils.isBlank(source) || StringUtils.isBlank(version) || StringUtils.isBlank(token)){
				return BaseResponse.error(-99, "header参数为空");
			}
			if(StringUtils.isBlank(info.getEsn())){
				return BaseResponse.error(-99, "ESN为空");
			}
			if(null == info.getLoadMsgId()){
				return BaseResponse.error(-99, "行程id为空");
			}
			if(null == info.getSuggestType()){
				return BaseResponse.error(-99, "是否有效为空");
			}

			boolean success = customerEngineRelLoadMsgRelationService.updateFuelTripSuggest(info);
			if(success){
				return BaseResponse.success("保存成功");
			} else {
				return BaseResponse.error(-99, "保存失败");
			}
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

}
